

// 路由
// Node 的路由  根据不同的接口地址(path) 返回对应的数据(html,text,json)

const http = require("http")
const hostname  = "0.0.0.0"
const port = 6600
const url = require("url")
const route = require("./route")

const server =http.createServer((req,res)=>{
    if(req.url!="/favicon.ico"){
        // console.log(req.url);
        const pathname = url.parse(req.url).pathname.replace(/\//,''); 
        console.log(pathname);
        // res.writeHead(200,{'content-type':'text/html;charset=utf8'});
        // if(pathname=="home"){
        //   res.write("<h2>Home - Home - 主页</h2>");
        // }else if(pathname=="login"){
        //   res.write("<h2>Login  - Login -  请登录</h2>");
        // }else if(pathname=="register"){
        //   res.write("<h2>Register  - Register -  立即注册</h2>");
        // }
        // res.end();

        try{  
          // route.home(req,res)
          // route.pathname(req,res)
          // route['home'](req,res)
          if(pathname.indexOf('images')==0){
            route['images'](req,res)
          }else{
            route[pathname](req,res)
          }
          
        }catch(err){
          console.log(err)
          route['notfound'](req,res)
        }
    }
}).listen(port,hostname,()=>{
  console.log("my server is running at "+ port)
})